package cn.hiapi.user.api;

import cn.hiapi.basic.ResUtils;
import cn.hiapi.basic.ResponseEntity;
import cn.hiapi.basic.controller.BasicController;
import cn.hiapi.basic.security.TokenGet;
import cn.hiapi.basic.token.Token;
import cn.hiapi.finance.entity.Amount;
import cn.hiapi.finance.query.AmountGet;
import cn.hiapi.finance.service.AmountService;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.interfaces.config.IConfigService;
import cn.hiapi.shopkeeper.consts.AppConst;
import cn.hiapi.shopkeeper.entity.activity.Activity;
import cn.hiapi.shopkeeper.entity.activity.ActivityOther;
import cn.hiapi.shopkeeper.service.Shopkeeper;
import cn.hiapi.shopkeeper.service.activity.ActivityFactory;
import cn.hiapi.shopkeeper.service.activity.ActivityOtherService;
import cn.hiapi.shopkeeper.service.activity.ActivityService;
import cn.hiapi.utils.JsonUtils;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * @author AdinZ
 * @date 2023/1/31 22:26
 */
@RestController
@RequestMapping("/user/amount")
public class ShopkeeperAmountController extends BasicController {

    private final DispatchContext dispatchContext;


    public ShopkeeperAmountController(DispatchContext dispatchContext) {
        this.dispatchContext = dispatchContext;
    }


    @ApiOperation(value = "喜糖抵扣", notes = "喜糖抵扣", nickname = "AdinZ")
    @GetMapping("/sugar")
    public ResponseEntity sugarAmount(@RequestParam("aid") long aid) {
        Token<Object> token = TokenGet.get();
        Amount amount = this.dispatchContext.getServiceOne(AmountService.class).amountGet(new AmountGet(token.getMerchantId(), token.getFid(), AppConst.FINANCE_AMOUNT_SUGAR));
        if (amount.getAmount().compareTo(BigDecimal.ZERO) <= 0) {
            return toResult(201, "没有可用喜糖");
        }
        ActivityOther other = this.dispatchContext.getServiceOne(ActivityOtherService.class).getByAid(aid);
        if (other == null || other.getSugar() != 1)
            return toResult(201, "不支持喜糖抵扣");
        JSONObject config = this.dispatchContext.getServiceOne(IConfigService.class).getConfig(token.getMerchantId(), AppConst.CONFIG_SHOPKEEPER_CONFIG);
        BigDecimal rate = JsonUtils.getBigDecimal(config, AppConst.CONFIG_SHOPKEEPER_CONFIG_SUGAR_RATE, BigDecimal.ONE);

        Activity activity = this.dispatchContext.getServiceOne(ActivityService.class).get(aid);
        if (activity == null) return toResult(201, "活动不存在");

        //最大可抵扣的 喜糖数量
        BigDecimal sugarDeduction = this.dispatchContext.getServiceOne(ActivityFactory.class).get(activity.getType()).sugarDeduction(activity, rate, amount.getAmount(), token.getFid());
        JSONObject data = new JSONObject();
        data.put("rate", rate);//喜糖比例
        data.put("sugarAmount", amount.getAmount()); //账户喜糖余额
        data.put("maxSugar", sugarDeduction); //最大抵扣喜糖约
        //最大抵扣系统, 大于余额
        BigDecimal money = Shopkeeper.sugarToMoney(sugarDeduction, rate);
        data.put("money", money);
        data.put("sugar", sugarDeduction);
        return toSuccess(data);
    }


}
